#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import shutil
import platform
import tkinter
from tkinter import ttk
from tkinter import filedialog
import time


dir_file = os.path.abspath('File_sorter.py')


text = f'''[Unit]
Description=filesorter service by PHoenjx

[Service]
Type=simple
ExecStart=/usr/bin/python3 {dir_file}

[Install]
WantedBy=multi-user.target
'''


main_window = tkinter.Tk()
main_window.title('Easy PHoenjx setup')
mainframe = ttk.Frame(main_window)
status_frame = ttk.Frame(mainframe, relief='sunken')

observe_path = tkinter.StringVar()
status = tkinter.StringVar(value='waiting for path')


def choose_directory():
    global observe_path
    observe_path.set(filedialog.askdirectory())
    status.set('All ready')
    install_button.state(['!disabled'])


def install():
    install_button.state(['disabled'])
    system_name = platform.platform()
    with open('File_sorter.py', 'r') as file:
        new_text = []
        for line in file:
            new_text.append(line)

        index = new_text.index("target_path = r''\n")
        index2 = new_text.index("sys_name = ''\n")
        new_text[index] = f"target_path = r'{os.path.normpath(observe_path.get())}'\n"
        new_text[index2] = f"sys_name = '{system_name}'\n"

    if 'Linux' in system_name:
        with open('File_sorter.py', 'w') as file:
            for line in new_text:
                file.write(line)

        with open('/lib/systemd/system/PHoenjx_filesorter.service', 'w') as file:
            file.write(text)
        os.system('sudo systemctl daemon-reload')
        os.system('sudo systemctl enable PHoenjx_filesorter.service')
        os.system('sudo systemctl start PHoenjx_filesorter.service')
        status.set('All done!')
        time.sleep(2)
        quit()
    elif 'Windows' in system_name:
        with open('File_sorter.py', 'w') as file:
            for line in new_text:
                file.write(line)
        user = os.getlogin()
        win_startup_path = os.path.normpath(
            f'C:/Users/{user}/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/')

        shutil.move(dir_file, os.path.join(win_startup_path, 'File_sorter.pyw'))
        status.set('All done!')
        time.sleep(2)
        os.startfile(os.path.join(win_startup_path, 'File_sorter.pyw'))
        time.sleep(3)
        quit()


choose_directory_label = ttk.Label(mainframe, text='Enter directory to sort:')
choose_directory_entry = ttk.Entry(mainframe, textvariable=observe_path)
choose_directory_button = ttk.Button(mainframe, text='Open', command=choose_directory)

status_label = ttk.Label(status_frame, textvariable=status)

install_button = ttk.Button(mainframe, text='Install/Update', command=install)
install_button.state(['disabled'])


mainframe.grid(column=0, row=0)

choose_directory_label.grid(column=0, row=0)
choose_directory_entry.grid(column=1, row=0)
choose_directory_button.grid(column=2, row=0)

status_frame.grid(column=0, row=1, columnspan=3)
status_label.pack()

install_button.grid(column=0, row=2, sticky='WE', columnspan=3)

main_window.mainloop()
